package com.qf.java2107.yb.hotel.controller.foodtype_bak;

import com.qf.java2107.yb.hotel.common.ResultVO;
import com.qf.java2107.yb.hotel.entity.BeanFactory;
import com.qf.java2107.yb.hotel.pojo.FoodType;
import com.qf.java2107.yb.hotel.service.FoodTypeService;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @author yb
 * @version 1.0
 * @date 2021-10-28
 **/
//@WebServlet("/foodtype/update")
public class FoodTypeUpdateController extends HttpServlet {

    private FoodTypeService foodTypeService = (FoodTypeService) BeanFactory.getBean("foodTypeService");

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        try {
            //如何快速封装前台请求参数到一个Java实体数据
            Map<String, String[]> parameterMap = request.getParameterMap();

            //实体模型
            FoodType foodType = new FoodType();

            //把map中的数据封装到一个实体模型中。有一个要求，map的key必须和Java实体属性名相同
            BeanUtils.populate(foodType, parameterMap);

            ResultVO vo = foodTypeService.update(foodType);

            if(vo.getSuccess()) {
                response.sendRedirect(request.getContextPath() + "/foodtype/search");
            } else {
                response.getWriter().write((String) vo.getData());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

}
